﻿@{
    Layout = "~/Views/Shared/_LayoutAdminPage.cshtml";
}

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">日志内容:</label>
                        <div class="layui-input-inline">
                            <input name="message" class="layui-input" placeholder="输入日志内容" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">异常内容:</label>
                        <div class="layui-input-inline">
                            <input name="exception" class="layui-input" placeholder="输入异常内容" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">级&emsp;别:</label>
                        <div class="layui-input-inline">
                            <select name="level">
                                <option value="">选择级别</option>
                                <option value="Error">Error</option>
                                <option value="Warning">Warning</option>
                                <option value="Information">Information</option>
                                <option value="Debug">Debug</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        &emsp;
                        <button class="layui-btn icon-btn" lay-filter="logRecordTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>&nbsp;
                        <button id="logRecordExportBtn" class="layui-btn icon-btn" type="button">
                            <i class="layui-icon">&#xe67d;</i>导出
                        </button>
                        <!-- 下拉按钮 -->
                        <div class="dropdown-menu dropdown-hover">
                            <button class="layui-btn icon-btn" type="button">
                                &nbsp;更多 <i class="layui-icon layui-icon-drop"></i>
                            </button>
                            <ul class="dropdown-menu-nav">
                                <li class="deleteallbtn"><a><i class="layui-icon layui-icon-delete"></i>清空日志</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="logRecordTable" lay-filter="logRecordTable"></table>
        </div>
    </div>
</div>

<script type="text/html" id="logTbBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    layui.use(['layer', 'form', 'table', 'util', 'laydate', 'tableX', 'dropdown', 'notice'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var tableX = layui.tableX;
        var dropdown = layui.dropdown;
        var notice = layui.notice;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#logRecordTable',
            url: '/api/admin/GetSystemLog',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                { type: 'checkbox' },
                { field: 'id',width:80},
                { field: 'Level', title: '等级', sort: true ,width:130},
                { field: 'Message', title: '日志内容', sort: true},
                { field: 'Exception', title: '异常内容', sort: true},
                {
                    field: 'Timestamp', title: '时间', templet: function (d) {
                        return util.toDateString(d.Timestamp);
                    }, sort: true
                },
                { title: '操作', toolbar: '#logTbBar', align: 'center', minWidth: 100,width:100 }
            ]]
        });


        /* 表格工具条点击事件 */
        table.on('tool(logRecordTable)', function (obj) {
            if (obj.event === 'del') { // 删除
                doDel(obj);
            }
        });
        /* 表格头工具栏点击事件 */
        table.on('toolbar(logRecordTable)', function (obj) {
            if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('logRecordTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', { icon: 2 });
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ ids: ids });
            }
        });


        /* 表格搜索 */
        form.on('submit(logRecordTbSearch)', function (data) {
            //if (data.field.loginRecordDateSel) {
            //    var searchDate = data.field.loginRecordDateSel.split(' - ');
            //    data.field.startDate = searchDate[0];
            //    data.field.endDate = searchDate[1];
            //} else {
            //    data.field.startDate = null;
            //    data.field.endDate = null;
            //}
            //data.field.loginRecordDateSel = undefined;
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 导出excel */
        $('#logRecordExportBtn').click(function () {
            var checkRows = table.checkStatus('logRecordTable');
            if (checkRows.data.length === 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                tableX.exportDataX({
                    cols: insTb.config.cols,
                    data: checkRows.data,
                    fileName: '系统日志'
                });
            }
        });

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                    var loadIndex = layer.load(2);
                  $.post('/api/admin/DeleteSystemLogByIds', {
                    ids: obj.data ? obj.data.id  : obj.ids.join(',')
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code == 1) {
                        layer.msg(res.message, { icon: 1 });
                        insTb.reload({ page: { curr: 1 } });
                    } else {
                        layer.msg(res.message, { icon: 2 });
                    }
                }, 'json');
            });
        }

        $('.deleteallbtn').click(function () {
            parent.layui.admin.confirm('确认清空吗？', function (index) {
                if (index) {
                    $.post("/api/admin/DeleteAllSystemLog", {}, function (res) {
                        res = JSON.parse(res);
                        parent.layer.close(index);
                        if (res.code == 1) {
                            notice.msg(res.message, { icon: 1 });
                        } else {
                            notice.msg(res.message, { icon: 3 });
                        }
                      
                    });
                }
               
               
            });
        });


    });
</script>